<template>
	<view class="content">
		<input class="row" type="text" v-model="userinfo.username" placeholder="在此输入账户">
		<input class="row" type="safe-password" v-model="userinfo.password" placeholder="在此输入密码">
		<button class="row" @click="getData">登录</button>
	</view>
</template>

<script>
	import {
		ref,
		toRefs,
		reactive
	} from 'vue';
	export default {
		setup() {

			const state = {
				userinfo: {
					username: "",
					password: ""
				}
			}

			const getData = () => {
				//点击按钮时，异步等待
				uni.showLoading({
					title: "正在请求..."
				})

				uni.request({
					url: "https://8c0ca265-f640-4eda-a886-92bd742a95fa.bspapp.com/http/user/login",
					method: 'POST',
					data: {
						username: state.userinfo.username,
						password: state.userinfo.password
					},
					success(res) {
						console.log(res);
						if (res.data.code == 2) {
							//本地保存token
							console.log(res.data.tokenId);
							uni.setStorageSync("tokenid", res.data.tokenId)
							uni.showToast({
								title: res.data.msg,
								duration: 3000,
								success() {
									uni.navigateTo({
										url: "/pages/ajaxget/ajaxget"
									})
								}
							})
						} else {
							uni.showToast({
								title: res.data.msg,
								duration: 3000
							})
						}
					},
					fail(err) {
						console.log(err);
					},
					complete() {
						console.log("请求完成");
						uni.hideLoading();
					}

				})
			}

			return {
				...toRefs(state),
				getData
			}

		}
	}
</script>

<style scoped>
	.content>.row {
		margin-top: 30rpx;
	}
</style>
